import Vue from 'vue'
import Vuex from 'vuex'
import user from './user'

Vue.use(Vuex)

export default new Vuex.Store({
  strict: true,
  // 状态即数据
  state: {
    count : 100,
    list: [1,2,3,4,5,6,7,8,9,10]
  },
  getters: {
    // $store.getters.filterList
    filterList(state, max) {
      return state.list.filter(item => item > max)
    }
  },
  // 只能通过mutation操作state
  // 必须是同步的
  mutations: {
    addCount(state, n) {
      state.count += n
    },
    removeCount (state) {
      state.count--
    },
    changeCount(state, count) {
      state.count = count
    }
  },
  // 异步操作
  actions: {
    setAsyncCount(context, count) {
      setTimeout(() => {
        context.commit('changeCount', count)
      }, 1000)
    }
  },
  // 其他js文件仓库
  modules: {
    // $store.state.user.age
    // 虽然分模块了, 但是user模块还是会挂载到index的state下
    user
  }
})
